package com.hm.homepage.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hm.homepage.entity.Website;
import com.hm.homepage.mapper.WebsiteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WebsiteService extends ServiceImpl<WebsiteMapper, Website> {

    @Autowired
    private WebsiteMapper websiteMapper;

    public List<Website> getAllWebsites() {
        return websiteMapper.selectList(null);
    }

    public List<Website> getWebsitesByCategory(String category) {
        return websiteMapper.selectByCategoryOrderByCreatedAtDesc(category);
    }

    public Website getWebsiteById(Long id) {
        return websiteMapper.selectById(id);
    }

    public Website saveWebsite(Website website) {
        website.setCreatedAt(LocalDateTime.now());
        websiteMapper.insert(website);
        return website;
    }

    public void deleteWebsite(Long id) {
        websiteMapper.deleteById(id);
    }

    public List<String> getAllCategories() {
        return websiteMapper.selectAllCategories();
    }
    
}